package com.blue.core.util;

import java.io.PrintWriter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * JSON 工具类
 * 
 * @author zhengj
 * @since 1.0 2016年11月17日
 */
public class JsonUtil
{
	private static SerializerFeature[] serializer = new SerializerFeature[] {
			SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue,
			SerializerFeature.WriteClassName};
		
	private static SerializerFeature[] outstream = new SerializerFeature[] {
			SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteMapNullValue};

	static
	{
		ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
	}

	private JsonUtil()
	{
	}
	
	/**
	 * 输出操作成功信息
	 * 
	 * @param pw 输出流
	 * @param objects 对象
	 */
	public static void outSuccess(PrintWriter pw, Object...objects)
	{
		String json = jsonSuccess(objects);
		try (PrintWriter p = pw)
		{
			p.print(json);
			p.flush();
		}
	}
	
	/**
	 * 输出操作失败信息
	 * 
	 * @param pw 输出流
	 * @param msg 失败信息
	 */
	public static void outError(PrintWriter pw, String msg)
	{
		String json = jsonError(msg);
		try (PrintWriter p = pw)
		{
			p.print(json);
			p.flush();
		}
	}
	
	/**
	 * 输出操作失败信息：<pre>发生错误，请联系管理员。</pre>
	 * 
	 * @param pw 输出流
	 */
	public static void outError(PrintWriter pw)
	{
		String json = jsonError(null);
		try (PrintWriter p = pw)
		{
			p.print(json);
			p.flush();
		}
	}

	/**
	 * 操作成功
	 *
	 * @param objects 对象列表
	 * @return json 字符串
	 */
	public static String jsonSuccess(Object...objects)
	{
		JSONObject json = new JSONObject();
		json.put("auth", true);
		json.put("session", true);
		json.put("state", true);
		for (int i = 0; i < objects.length; i++)
		{
			json.put("content" + i, objects[i]);
		}
		return JSON.toJSONString(json, outstream);
	}

	/**
	 * 操作失败
	 *
	 * @param msg 错误信息，可空
	 * @return json 字符串
	 */
	public static String jsonError(String msg)
	{
		if (msg == null || msg.isEmpty())
			msg = "发生错误，请联系管理员。";

		JSONObject json = new JSONObject();
		json.put("auth", true);
		json.put("session", true);
		json.put("state", false);
		json.put("msg", msg);
		return json.toJSONString();
	}
	
	/**
	 * 输出JSON格式字符串
	 * 
	 * @param pw 输出流
	 * @param json JSON格式字符串
	 */
	public static void out(PrintWriter pw, String json)
	{
		try (PrintWriter p = pw)
		{
			p.print(json);
			p.flush();
		}
	}
	
	/**
	 * 把对象序列化成json格式字符串，带@type类型
	 * 
	 * @param object 对象
	 * @return json字符串
	 */
	public static String encode(Object object)
	{
		String json = JSON.toJSONString(object, serializer);
		return json;
	}
	
	/**
	 * 把对象序列化成json格式字符串
	 * 
	 * @param object 对象
	 * @return json字符串
	 */
	public static String output(Object object)
	{
		String json = JSON.toJSONString(object, outstream);
		return json;
	}
	
	/**
	 * 把json格式字符串反序列化成对象
	 * 
	 * @param <T> 对象类型
	 * @param json json字符器
	 * @param clazz 对象的类型
	 * @return 实例化的对象
	 */
	public static <T> T decode(String json, Class<T> clazz)
	{
		T obj = JSON.parseObject(json, clazz);
		return obj;
	}
	
}
